###########################################################
# $Id: xrsh_cc.sh 4686 2013-01-07 20:22:56Z xrdk $
# brief	    : 
# author    : xrdk
# version   : V1.0, 2004-03-16
#
# CopyRight (C) 2004-2011 xrdk.
# All rights reserved.
###########################################################

function xrmk_scc_supped()
{
	local _xrmk_arr=(xlc xlcpp acc suncc watcom)
	
	if [ "$1" = "get" ]; then
		echo -S spec. compiler supped: ${_xrmk_arr[*]}
	  echo "--    xlc : use xlc_r on aix"
	  echo "--  xlcpp : use xlC_r on aix"
	  echo "--    acc : use aCC on hpux"
	  echo "--  suncc : use suncc on sun"
	  echo "-- watcom : use watcom on qnx 4"
		return
	fi
	
	local _xrmk_tmp=`echo ${_xrmk_arr[*]}`
	_xrmk_tmp=`xrsh_hasitem "$_xrmk_tmp" $1`
	
	if test "$_xrmk_tmp" = "no"; then
		echo "Unsupp compiler: $1"
		xrmk_scc_supped "get"
		exit 1
	fi
}

function xrmk_scc_valid()
{
	if [ "$1" = "xlc" ] || [ "$1" = "xlcpp" ]; then
		if [ "$2" != "aix" ]; then
			echo "xlc or xlcpp must on aix: $2"
			exit 1
		fi	
	fi
	
	if [ "$1" = "acc" ] && [ "$2" != "hpux" ]; then
		echo "acc must on hpux: $2"
		exit 1
	fi
	
	if [ "$1" = "suncc" ] && [ "$2" != "sun" ]; then
		echo "suncc must on sun: $2"
		exit 1
	fi
	
	if [ "$1" = "watcom" ] && [ "$2" != "qnx" ]; then
		echo "watcom must on qnx: $2"
		exit 1
	fi
}

function xrmk_cc_echo()
{
	echo ---------- compiler info -------------------
	echo "--   home dir. : $xrmk_cc_home"
	echo "--    bin dir. : $xrmk_cc_home_bin"
	echo "--           c : $xrmk_cc"
	echo "--         c++ : $xrmk_cpp"
	echo "--        make : $xrmk_make"
	echo "--       gmake : $xrmk_gmake"
	echo "--          ar : $xrmk_ar"
	echo "--     version : $xrmk_cc_ver"
	echo "--    ver. maj : $xrmk_cc_vmaj"
	echo
  echo "--    ar flags : $xrmk_flags_ar"
  echo "-- debug flags : $xrmk_flags_debug"
	echo "--       dso c : $xrmk_flags_dso_c"
  echo "--     dso lnk : $xrmk_flags_dso_lnk"
  echo "--      c hide : $xrmk_flags_hide"
  echo "--     as need : $xrmk_flags_asneed"
  echo "--    32 flags : $xrmk_flags_32_c"
  echo "--    64 flags : $xrmk_flags_64_c"
  echo "--      32 lnk : $xrmk_flags_32_lnk"
  echo "--      64 lnk : $xrmk_flags_64_lnk"
  echo
  echo "-- system libs : $xrmk_lib_sys"
  echo
  echo "--   rpath pre : $xrmk_rpath_pre"
  echo "--   rpath sub : $xrmk_rpath_sub"
  echo "--       rpath : $xrmk_runpath"
  echo
  echo --------------------------------------------
}

function xrmk_gcc_get()
{
	xrmk_cc_name="gcc"
	case $1 in
	  lnx|linux)  xrmk_get_gcc_lnx      ;;
	  sun)	      xrmk_get_gcc_sun	    ;;
	  aix)	      xrmk_get_gcc_aix	    ;;
	  hpux)	      xrmk_get_gcc_hpux     ;;
	  osx)	      xrmk_get_gcc_osx	    ;;
	  freebsd)	  xrmk_get_gcc_freebsd  ;;
	  qnx)	      xrmk_get_gcc_qnx	    ;;
	  *)	    ;;
	esac
}

function xrmk_scc_get()
{
	xrmk_cc_name="$1"
	local _xrmk_tmp=$1
	if [ "$_xrmk_tmp" = "xlc" ]; then
		xrmk_get_scc_xlc
	elif [ "$_xrmk_tmp" = "xlcpp" ]; then
		xrmk_get_scc_xlcpp
	elif [ "$_xrmk_tmp" = "acc" ]; then
		xrmk_get_scc_acc
	elif [ "$_xrmk_tmp" = "suncc" ]; then
		xrmk_get_scc_suncc
	elif [ "$_xrmk_tmp" = "watcom" ]; then
		xrmk_get_scc_watcom
	fi
}
#=====================================================================
#=====================================================================
#Get cc home and bin dir. Args: 1 cc name; 2 alt. home path.
#xrmk_cc_get_path "gcc" "/usr" 
function xrmk_cc_get_path()
{
	if [ "$1" = "" ]; then return; fi
	
	#if set home, dont auto check
	if [ "$xrmk_cc_home" != "" ]; then
		xrmk_cc_home_bin="$xrmk_cc_home/bin"
		xrmk_cc="$xrmk_cc_home_bin/$1"
		return
	fi
	
	#if cc home path invalid, set default "/usr"
	local _xrmk_tmp="$2"
	if [ "$_xrmk_tmp" = "" ]; then _xrmk_tmp="/usr" ; fi
	
	#if get cc failed, use default cc: cchomepath/bin/ccname
	if [ "$xrmk_cc" = "" ]; then xrmk_cc=`xrsh_getexepath "$1" "$_xrmk_tmp/bin/$1"`; fi
	
	xrmk_cc_home_bin=`xrsh_getparentdir "$xrmk_cc"`
	xrmk_cc_home=`xrsh_getparentdir "$xrmk_cc_home_bin"`
}
#=====================================================================
#=====================================================================
function xrmk_cc_get_default()
{
	#common flags for compile dso and lib
	xrmk_flags_c="-O0 -Wall -fsigned-char -D_GNU_SOURCE -D_THREAD_SAFE -D_REENTRANT"
	
	xrmk_flags_ar="-r"
	xrmk_flags_debug="-g"
	
	#flags for compile and link dso
	xrmk_flags_dso_c="-fpic"
	xrmk_flags_dso_lnk="-shared"
	
	#rpath opt
	xrmk_rpath_pre="-Wl,-rpath="
	xrmk_rpath_sub=
	
	#link prefix and subfix for lib
	xrmk_lib_pre="-l"
	xrmk_lib_sub=
	
	xrmk_lib_sys="-ldl -lpthread -lm -lrt"
}
#=====================================================================
function xrmk_get_gcc_lnx()
{
  xrmk_cc_get_path "gcc" "/usr"

	xrmk_flags_32_c="-m32"
	xrmk_flags_64_c="-m64"
	xrmk_flags_32_lnk="-m32"
	
	xrmk_lib_pre=usedir
	xrmk_lib_sub=useext
}
#=====================================================================
function xrmk_get_gcc_aix()
{
  xrmk_cc_get_path "gcc" "/usr"
  
	xrmk_has_rpath=no
	
	xrmk_flags_hide=no
	xrmk_flags_asneed=no

	xrmk_lib_pre=usedir
	xrmk_lib_sub=useext
	
	xrmk_flags_32_c="-maix32"
	xrmk_flags_64_c="-maix64"
	xrmk_flags_64_lnk="-maix64"
	xrmk_set_c_env_32="export OBJECT_MODE=32"
	xrmk_set_c_env_64="export OBJECT_MODE=64"
}
#=====================================================================
function xrmk_get_gcc_sun()
{
	if [ $xrmk_os_vmaj$xrmk_os_vmin -gt "58" ]; then
		xrmk_cc_get_path "gcc" "/usr/sfw"
	else
		xrmk_cc_get_path "gcc" "/opt/sfw"
		xrmk_lib_pre=usedir
		xrmk_lib_sub=useext
	fi
	
	xrmk_flags_dso_lnk="-G"
	
	xrmk_rpath_pre="-R "
	xrmk_rpath_sub=
	
	xrmk_lib_sys="-ldl -lpthread -lm -lrt -lsocket -lnsl"
}
#=====================================================================
function xrmk_get_gcc_hpux()
{
  xrmk_cc_get_path "gcc" "/opt/hp-gcc"
  
	xrmk_has_rpath=no
	
	xrmk_flags_asneed=no
	
  xrmk_lib_pre=usedir
  xrmk_lib_sub=useext

	xrmk_flags_32_c="-mlp32"
	xrmk_flags_64_c="-mlp64"
}
#=====================================================================
function xrmk_get_gcc_osx()
{
	xrmk_cc_get_path "gcc" "/usr"
	
  xrmk_has_rpath=no
  
  xrmk_lib_pre=usedir
  xrmk_lib_sub=useext
  
 	xrmk_flags_hide=no
	xrmk_flags_asneed=no
 
  xrmk_lib_sys="-lm"
}
#=====================================================================
function xrmk_get_gcc_freebsd()
{
  xrmk_cc_get_path "gcc" "/usr"
	xrmk_lib_sys="-lpthread -lm"
}
#=====================================================================
function xrmk_get_gcc_qnx()
{
	xrmk_cc_get_path "gcc" "/usr"
	
	xrmk_lib_sys="-lm"
	
	xrmk_rpath_pre=
	xrmk_rpath_sub=
	
	xrmk_get_cc_ver="$xrmk_cc --version"
	xrmk_cc_ver=`$xrmk_get_cc_ver | sed -n 's/\([0-9]\{1,10\}\.[0-9]\{1,10\}\.[0-9]\{1,10\}\).*/\1/p'`
	xrmk_cc_vmaj=`xrsh_getdotnum $xrmk_cc_ver`
}
#=====================================================================
#=====================================================================
function xrmk_get_scc_xlc()
{
  xrmk_cc_get_path "xlc_r" "/usr/vac"
  
  xrmk_flags_c="-qchars=signed -qcpluscmt -qmaxmem=8192 -qOPT=0"
	xrmk_flags_dso_c=
	xrmk_flags_dso_lnk="-G -brtl -bM:SRE"
	xrmk_flags_ld="-brtl -bM:SRE"
	
	xrmk_rpath_pre="-blibpath:"
	xrmk_rpath_sub=
	
	xrmk_get_cc_ver="$xrmk_cc -qversion"
	xrmk_cc_ver=`$xrmk_get_cc_ver | sed -n 's/.*V\([0-9]\{1,10\}\.[0-9]\{1,10\}\)/\1/p'`
}

function xrmk_get_scc_xlcpp()
{
  xrmk_cc_get_path "xlC_r" "/usr/vacpp"
    
  xrmk_flags_c="-qchars=signed -flag=w:e -qcpluscmt -qmaxmem=8192 -qOPT=0"
	xrmk_flags_dso_c=
	xrmk_flags_dso_lnk="-G -brtl -bM:SRE"
	xrmk_flags_ld="-brtl -bM:SRE"
	
	xrmk_has_rpath=no
	
	xrmk_get_cc_ver="$xrmk_cc -qversion"
	xrmk_cc_ver=`$xrmk_get_cc_ver | sed -n 's/.*V\([0-9]\{1,10\}\.[0-9]\{1,10\}\)/\1/p'`
}
#=====================================================================
function xrmk_get_scc_suncc()
{
	xrmk_cc_get_path "cc" "/usr/sfw"
	
	xrmk_flags_dso_c="-KPIC"
	xrmk_flags_dso_lnk="-g"
	
	xrmk_rpath_pre="-R "
	xrmk_rpath_sub=
	
	xrmk_flags_32_c="-m32"
	xrmk_flags_64_c="-m64"
	
	xrmk_lib_sys="-ldl -lpthread -lm -lrt -lsocket -lnsl"
	
	xrmk_get_cc_ver="$xrmk_cc -V"
	xrmk_cc_ver=`$xrmk_get_cc_ver | sed -n 's/.*\([0-9]\{1,10\}\.[0-9]\{1,10\}\) *.*/\1/p'`
}
#=====================================================================
function xrmk_get_scc_acc()
{
	xrmk_cc_get_path "cc" "/usr"
	
	xrmk_flags_debug="-g0"
	xrmk_flags_dso_c="+z"
	xrmk_flags_dso_lnk="-b"
	
	xrmk_has_rpath=no
	#xrmk_rpath_pre="-Wl,+b "
	#xrmk_rpath_sub=" -Wl,+s"
	
	xrmk_lib_pre=usedir
	xrmk_lib_sub=useext
	
	xrmk_lib_sys="-ldl -lpthread -lm -lnsl -ldld"
	
	xrmk_get_cc_ver="$xrmk_cc --version"
	#xrmk_cc_ver=`$xrmk_get_cc_ver | sed -n 's/*.\.\([0-9]\{1,10\}\.[0-9]\{1,10\}\) *.*/\1/p'`
}
#=====================================================================
function xrmk_get_scc_watcom()
{
	xrmk_cc_get_path "cc" "/"
	
	xrmk_flags_c="-j -w3 -Od"
	xrmk_flags_ar="-n -q"
	xrmk_flags_dso_lnk="-G"
	
	xrmk_has_rpath=no
	
	xrmk_lib_sys="-lm"
}
#=====================================================================
